﻿var options = {};
//var effect = ["blind","bounce","clip","drop","explode","fade","fold","highlight","puff","pulsate","scale","shake","size","slide","transfer"];
var effect = ["fold"];
var effects = effect[Math.floor(Math.random()*effect.length)];

function loadEffect(msg){
	//alert(msg);
	//$("#main_panel").ajaxSend(function(){
	//	$(this).html('Loading ... <img src="images/loading.gif">');
	//});
	$("#main_panel").effect( effects, options, 500, callback(msg) );
}

function callback(msg) {
	setTimeout(function() {
		$( "#main_panel" ).removeAttr( "style" ).hide().fadeIn().html(msg);	
	}, 500 );
};

function numbersonly(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
		if(unicode == 46){
			return true;
		}
		if ((unicode<48||unicode>57)){ //if not a number
			return false; //disable key press
		}
	}
}

function orderExport(page){

	loadEffect("Loading...");
	$.ajax({
	   type: "POST",
	   url: "order_export.php",
	   data: "&page="+page+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg);		   
	   }
	});
}

function newOrderExport(id){
	loadEffect("Loading...");
	$.ajax({
	   type: "POST",
	   url: "order_export_form.php",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg);		   
	   }
	});
}

function viewOrderExport(id){
	loadEffect("Loading...");
	$.ajax({
	   type: "POST",
	   url: "vieworderexport.php",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg);		   
	   }
	});
}

function dealerManage(){
	loadEffect("Loading...");
	$.ajax({
	   type: "POST",
	   url: "dealerManage.php",
	   data: "&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg);		   
	   }
	});
}

function test(){
	var str= "orderi:1";
	alert(str.match("orderid:"));
}

function Order(){
	loadEffect("Loading...");
	$.ajax({
	   type: "POST",
	   url: "order.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg);		   
	   }
	});
}

function Order2(){
	loadEffect("Loading...");
	$.ajax({
	   type: "POST",
	   url: "order2.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg);		   
	   }
	});
}

function Product_Main(){	
	$.ajax({
	   type: "POST",
	   url: "product_main.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function loadCatManage(){
	$.ajax({
	   type: "POST",
	   url: "category_manage.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function frmAddCategory(){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmAddCategory",
	   data: "&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function addCategory(){
	var cat_name = $("#cat_name").val();
	document.getElementById('btnAddCategory').disabled = true;

	if(cat_name != ""){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=addCategory",
		   data: "&cat_name="+cat_name+"&t="+Math.random(),
		   success: function(msg){
			   alert(msg);
			   document.getElementById('btnAddCategory').disabled = false;
			   loadCatManage();
		   }
		});
	}else{
		alert("Please type Category Name.");
	}

}

function frmEditCategory(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmEditCategory",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function editCategory(id){
	var name = $("#cat_name").val();
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=editCategory",
	   data: "&id="+id+"&name="+name+"&t="+Math.random(),
	   success: function(msg){
			alert(msg);
			loadCatManage();
	   }
	});
}

function loadProManage(){
	$.ajax({
	   type: "POST",
	   url: "product_manage.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg);
	   }
	});
	
}

function frmAddProduct(){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmAddProduct",
	   data: "&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function clickupload(){
	if ( document.getElementById('fileupload').value.length == 0 ){
		alert( 'ระบุ File ที่จะ Upload สิจ๊ะ' ) ;
		return false ;
	}
	$("#frm_pro_pic").hide();
	$("#upmsg").html('กำลัง Upload อยู่นะจ๊ะ รอซักครู่...' );
	document.getElementById('btnUpload').value = "ส่งไปแล้วไม่ต้องคลิกซ้ำ";
	document.getElementById('btnUpload').disabled = true ;

	return true ;
}

function uploadok(pathfile){
	$("#pro_pic").val(pathfile);
	$("#upmsg").html('<a href="'+pathfile+'" target="_blank">'+pathfile+'</a>');
	document.getElementById('btnUpload').value = "Upload เลยเด่ะ";
	document.getElementById('btnUpload').disabled = false;
	document.getElementById('frmUpload').reset() ;
	$("#frm_pro_pic").show();
	return true ;
}


function addProduct(){
	var barcode = $("#barcode").val();
	var name = $("#pro_name_th").val();
	var namemm = $("#pro_name_mm").val();
	var tmp = $("#pro_cat").val().split(":");
	var image = $("#pro_pic").val();

	var cat = tmp[0];


	document.getElementById('btnAddProduct').disabled = true;
	
	if(name != "" || cat != ""){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=addProduct",
		   data: "&barcode="+barcode+"&nameth="+name+"&namemm="+namemm+"&cat="+cat+"&image="+image+"&t="+Math.random(),
		   success: function(msg){
			   alert(msg);
			   document.getElementById('btnAddProduct').disabled = false;
			   loadProManage();
		   }
		});
	}else{
		alert("Please type Product Name or Category Name.");
	}
}

function frmEditProduct(id){
	
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmEditProduct",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   //loadEffect(msg);
		   //alert(msg);
		   $("#dialogEditProduct").html(msg);
		   $("#dialogEditProduct").dialog({ width: 460 });
	   }
	});
	
	return false;
}

function closeEditProduct(){
	$("#dialogEditProduct").dialog('close');
}

function editProduct(id){
	var barcode = $("#barcode").val();
	var nameth = $("#pro_name_th").val();
	var namemm = $("#pro_name_mm").val();
	var cat = $("#pro_cat").val();
	var image = $("#pro_pic").val();
	var unit = $("#pro_unit").val();
	var pic_edit = $("#pro_pic_edit").val();
	var target = $("#pro_target").val();
	
	document.getElementById('btnEditProduct').disabled = true;

	if(name != "" || cat != ""){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=editProduct",
		   data: "&id="+id+"&barcode="+barcode+"&nameth="+nameth+"&namemm="+namemm+"&cat="+cat+"&image="+image+"&pic_edit="+pic_edit+"&unit="+unit+"&target="+target+"&t="+Math.random(),
		   success: function(msg){
			   showInformation(msg);
			   document.getElementById('btnEditProduct').disabled = false;
			   closeEditProduct();
			   //alert(id);
			   loadProductTable(id);
			   //loadProManage();
		   }
		});
	}else{
		alert("Please type Product Name or Category Name.");
	}
}

function showInformation(msg){
	$("#dlg_show_information_txt").html(msg);
	$("#dlg_show_information").dialog({ buttons: { "Ok": function() { $(this).dialog("close"); } } });
	$("#dlg_show_information").dialog({ width: "200" });	
}

function closeInformation(){
	$("#dlg_show_information").dialog('close');
}

function loadProductTable(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=loadProductTable",
	   data: "&id="+id+"t="+Math.random(),
	   success: function(msg){
			$('#tbl_product_'+id).html(msg);
	   }
	});
}

function loadUnitManage(){
	$.ajax({
	   type: "POST",
	   url: "unit_manage.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function frmAddUnit(){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmAddUnit",
	   data: "&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function addUnit(){
	var unit_name = $("#unit_name").val();
	document.getElementById('btnAddUnit').disabled = true;

	if(unit_name != ""){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=addUnit",
		   data: "&unit_name="+unit_name+"&t="+Math.random(),
		   success: function(msg){
			   alert(msg);
			   document.getElementById('btnAddUnit').disabled = false;
			   loadUnitManage();
		   }
		});
	}else{
		alert("Please type Unit Name.");
	}

}

function frmEditUnit(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmEditUnit",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function editUnit(id){
	var name = $("#unit_name").val();
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=editUnit",
	   data: "&id="+id+"&name="+name+"&t="+Math.random(),
	   success: function(msg){
			alert(msg);
			loadUnitManage();
	   }
	});
}

function addOrderItem(){
	var or_item = $("#or_item").val();
	var or_amount = $("#or_amount").val();
	var or_itemname = $("#or_itemname").val();
	var or_unit = $("#or_unit").val();
	var or_unittext = $("#or_unittext").val();
	var or_quick = $("#quick").attr('checked');	
	var item = "";
	var chk = true;
	var className = "";

	if(or_quick == "checked"){
		or_quick = "1";
	}else{
		or_quick = "0";
	}

	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=addOrderCart",
	   data: "&or_item="+or_item+"&or_amount="+or_amount+"&or_itemname="+or_itemname+"&or_unit="+or_unit+"&or_unittext="+or_unittext+"&or_quick="+or_quick+"&t="+Math.random(),
	   success: function(msg){
			if(msg.match("ok:")){
				updateCart();
			}else{
				alert(msg);
			}
		}
	});
}

function getOrderForm(id){
	$.ajax({
		type: "POST",
		url: "process.php?cmd=getOrderForm",
		data: "&or_item="+id+"&t="+Math.random(),
		success: function(msg){
			$("#addCart").html(msg);
			$("#addCart").dialog({
				height: "auto", 
				width: "auto", 
				modal: true,
				buttons: {
					Ok: function() {
						//addOrderItem();
						addOrderItem();
						$( this ).dialog( "close" );
					}					
				}
			});
		}
	});
}

function testOrder(){
	var or_item = $("#or_item").val();
	var or_amount = $("#or_amount").val();
	var or_itemname = $("#or_itemname").val();
	var or_unit = $("#or_unit").val();
	var or_unittext = $("#or_unittext").val();
	var or_quick = $("#quick").attr('checked');	
	var item = "";
	var chk = true;
	var className = "";

	if(or_quick == "checked"){
		or_quick = "1";
	}else{
		or_quick = "0";
	}
	//alert(or_item+":"+or_amount+":"+":"+or_itemname+":"+or_unit+":"+or_unittext+":"+or_quick);
}
function updateCart(){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=loadCart",
	   data: "t="+Math.random(),
	   success: function(msg){
		   //alert(msg);
			$('#order_show').html(msg);
			$("#checkOutBtn").css("display","inline");
			//alert($("#or_list").html());
	   }
	});
}

function oldaddOrderItem(){
	var or_item = $("#or_item").val();
	var or_amount = parseInt($("#or_amount").val());
	var or_itemname = $("#or_itemname").val();
	var or_index = $("#or_index").val();
	var or_unit = $("#or_unit").val();
	var or_unittext = $("#or_unittext").val();
	var or_quick = $("#quick").attr('checked');	
	var item = "";
	var chk = true;
	var className = "";

	if(or_quick == "checked"){
		or_quick = "1";
		classname = "aQ";
	}else{
		or_quick = "0";
		classname = "a";
	}
	
	if(or_item != "" && or_amount > 0 && or_unit != ""){
		$("#or_list").css("display","inline");
	
		$("input[name^=or_i]").each(function(i) {
			var xval = $(this).val();
			if(or_item == xval){
				var lastVal = parseInt($("input[name^=or_a]").eq(i).val());
				or_amount += lastVal;
				var tr_id = $(this).parents("tr").attr("id");			
				item = '<tr id="tr_'+or_index+'"><td class="'+classname+'">'+or_itemname+'</td><td class="b">'+or_amount+'</td><td class="c">'+or_unittext+"</td>";
				item += '<td align="center"><input type="hidden" name="or_i[]" value="'+or_item+'">';
				item += "<input type='hidden' name='or_a[]' value='"+or_amount+"'>";
				item += "<input type='hidden' name='or_u[]' value='"+or_unit+"'>";
				item += "<input type='hidden' name='or_q[]' value='"+or_quick+"'>";
				item += '<a href="#" onclick="delOrItem(\''+or_index+'\')">del</a></td></tr>';
				$('#'+tr_id).replaceWith(item);
				chk = false;
			}
		});
		
		if(chk){
			item = '<tr id="tr_'+or_index+'"><td class="'+classname+'">'+or_itemname+'</td><td class="b">'+or_amount+'</td><td class="c">'+or_unittext+"</td>";
			item += '<td align="center"><input type="hidden" name="or_i[]" value="'+or_item+'">';
			item += "<input type='hidden' name='or_a[]' value='"+or_amount+"'>";
			item += "<input type='hidden' name='or_u[]' value='"+or_unit+"'>";
			item += "<input type='hidden' name='or_q[]' value='"+or_quick+"'>";
			item += '<a href="#" onclick="delOrItem(\''+or_index+'\')">del</a></td></tr>';
			$('#or_list tr:first').next().after(item);
			$('#or_list tr:first').next().next().hide().fadeIn("slow")
		}
		$("#or_index").val(parseInt(or_index)+1);
		document.getElementById('btnCheckOut').disabled = false;
		$("#or_amount").val("");
		$("#quick").attr('checked',false);
					
		//alert($("#or_list").html());
	}else{
		alert("Invalid Value");
	}
}

function updateOrCat(){
	var catid = $("#or_cat").val();
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=updateOrCat",
	   data: "&catid="+catid+"&t="+Math.random(),
	   success: function(msg){
			$("#showOrItem").html(msg);
			$("#showdetail1").show();
			$("#showdetail2").show();
	   }
	});
}

function updateOrName(){
	$("#or_item option:selected").each(function() {
		$("#or_itemname").val($(this).text());
		
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=getUnitcall",
		   data: "&id="+$("#or_item option:selected").val()+"t="+Math.random(),
		   success: function(msg){
			   if(msg != ""){
					var id = msg.split(":");
					$('#or_unit option').removeAttr('selected').filter('[value=' + id[0] + ']').attr('selected', true);    
					$("#or_unittext").val(id[1]);
					$("#or_amount").val("");
					$("#or_amount").focus();
			   }
		   }
		});
	});
}

function updateOrUnit(){
	$("#or_unit option:selected").each(function() {
		$("#or_unittext").val($(this).text());
	});
}


function delOrItem(index){

	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=removeCart",
	   data: "&id="+index+"&t="+Math.random(),
	   success: function(msg){
			if(msg.match("ok:")){
				$('#tr_'+index).remove();
				var rowCount = $('#or_lists tr').length;
				if(rowCount == 2){
					$("#or_lists").css("display","none");
					$("#checkOutBtn").css("display","none");
				}
			}else{
				alert(msg);
			}
		}
	});
}

function orderCheckOut(){
	if(confirm('Do you want to Order ?')==true){

		var item = new Array();
		var amount = new Array();
		var unit = new Array();
		var quick = new Array();

		$("input[name^=or_i]").each(function(i) {
			//alert(i+":"+$(this).val());
			item[i] = $(this).val();
		});

		$("input[name^=or_a]").each(function(i) {
			//alert(i+":"+$(this).val());
			amount[i] = $(this).val();
		});

		$("input[name^=or_u]").each(function(i) {
			//alert(i+":"+$(this).val());
			unit[i] = $(this).val();
		});

		$("input[name^=or_q]").each(function(i) {
			//alert(i+":"+$(this).val());
			quick[i] = $(this).val();
		});

		//alert(item.length);
		if(item.length > 0){
			$.ajax({
			   type: "POST",
			   url: "process.php?cmd=addCart",
			   data: { "item[]" : item , "amount[]" : amount , "unit[]" : unit , "quick[]" : quick , "t" : Math.random() },
			   success: function(msg){
				   if(msg.match("orderid:")){
						alert("Success");
						var id = msg.split(":");
						loadEffect('Download Order <a href="excel.php?id='+id[1]+'"><img src="images/excel_icon.jpg" width="20"></a>');
						//getExcelForm();
				   }else{
						alert(msg);
				   }
					//loadUnitManage();
			   }
			});
		}else{
			alert("No Data");
		}
	}
}

function getExcelForm(){
	$.ajax({
	   type: "POST",
	   url: "excel.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function History(){
	//$("#main_panel").effect( effects, options, 500, callback(msg) );
	$.ajax({
	   type: "POST",
	   url: "history.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   //loadEffect(msg);
		   $("#main_panel").effect( effects, options, 500, callback(msg) );
	   }
	});
}

function Logoshop_Main(){	
	$.ajax({
	   type: "POST",
	   url: "logoshop_main.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function updateLogoMain(){	
	$.ajax({
	   type: "POST",
	   url: "logoshop_main.php?cat_id="+$("#catLogo").val(),
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function frmNewLogoshop(){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmNewLogoshop",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function newLogoProduct(){
	var barcode = $("#log_code").val();
	var category = $("#log_cat").val();
	var name = $("#log_name").val();
	var price = $("#log_price").val();

	if(valNewPro()){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=addNewLogoshop",
		   data: "&code="+barcode+"&cat="+category+"&name="+name+"&price="+price+"&t="+Math.random(),
		   success: function(msg){
				alert(msg);
				Logoshop_Main();
		   }
		});
	}
}

function valNewPro(){

	var barcode = $("#log_code").val();
	var category = $("#log_cat").val();
	var name = $("#log_name").val();
	var price = $("#log_price").val();
	
	if(barcode.length < 1){
		alert("Empty Barcode !!!");
		return false;
	}

	if(name.length < 1){
		alert("Product Name ...... ?");
		return false;
	}

	if(price.length < 1){
		alert("No Price ?");
		return false;
	}
	return true;
}

function chkNumber(ele){
	var vchar = String.fromCharCode(event.keyCode);
	if ((vchar<'0' || vchar>'9') && (vchar != '.')) return false;
	ele.onKeyPress=vchar;
}

function frmeditLogo(barcode){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmeditLogo",
	   data: "&barcode="+barcode+"&t="+Math.random(),
	   success: function(msg){
		   //loadEffect(msg);
		   //alert(msg);
		   $("#dialogEditLogo").html(msg);
		   $("#dialogEditLogo").dialog({ width: 460 });
	   }
	});
}

function editLogoProduct(){
	var oldCode = $("#old_code").val();
	var barcode = $("#st_code").val();
	var st_cat = $("#st_cat").val();
	var st_name = $("#st_name").val();
	var st_price = $("#st_price").val();

	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=editLogoProduct",
	   data: "&oldCode="+oldCode+"&barcode="+barcode+"&st_cat="+st_cat+"&st_name="+st_name+"&st_price="+st_price+"&t="+Math.random(),
	   success: function(msg){
		   $("#dialogEditLogo").dialog('close');
		   alert(msg);
		   Logoshop_Main();
	   }
	});

}

function frmBuyStock(){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=frmBuyStock",
	   data: "&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function addBuyStock(){
	var barcode = $("#buyBarcode").val().trim();
	var amount = $("#buyAmount").val();

	var st_code = barcode;
	var st_amount = parseInt(amount);


	var st_index = $("#st_index").val();
	var item = "";
	var chk = true;

	var pname = "";
	$("#st_pname").load("process.php?cmd=getPName&id="+barcode+"&t="+Math.random());
	
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=getPName",
	   data: "&id="+barcode+"&t="+Math.random(),
	   success: function(msg){

		   pname = msg;

			if(barcode != "" && amount > 0){
				$("#tblStock").css("display","inline");
				
				$("input[name^=st_i]").each(function(i) {
					var xval = $(this).val();
					if(st_code == xval){
						var lastVal = parseInt($("input[name^=st_a]").eq(i).val());
						st_amount += lastVal;
						var tr_id = $(this).parents("tr").attr("id");			
						item = '<tr id="tr_'+st_index+'"><td class="a">'+st_code+" | "+pname+'</td><td class="b">'+st_amount+'</td>';
						item += '<td align="center"><input type="hidden" name="st_i[]" value="'+st_code+'">';
						item += "<input type='hidden' name='st_a[]' value='"+st_amount+"'>";
						item += '<a href="#" onclick="delStItem(\''+st_index+'\')">del</a></td></tr>';
						$('#'+tr_id).replaceWith(item);
						chk = false;
					}
				});
				
				if(chk){
					item = '<tr id="tr_'+st_index+'"><td class="a">'+st_code+" | "+pname+'</td><td class="b">'+st_amount+'</td>';
					item += '<td align="center"><input type="hidden" name="st_i[]" value="'+st_code+'">';
					item += "<input type='hidden' name='st_a[]' value='"+st_amount+"'>";
					item += '<a href="#" onclick="delStItem(\''+st_index+'\')">del</a></td></tr>';
					$('#tblStock tr:first').next().after(item);
					$('#tblStock tr:first').next().next().hide().fadeIn("slow")
				}

				$("#st_index").val(parseInt(st_index)+1);
				document.getElementById('btnStCheckOut').disabled = false;
				$("#buyBarcode").val("");
				$("#buyAmount").val("");
				$("#buyBarcode").focus();
							
				//alert($("#or_list").html());
			}else{
				alert("Invalid Value");
			}


	   }
	});
	
//alert($("#st_pname").val());
	//alert(barcode + " : " + amount);
	
}

function delStItem(index){
	//alert("Delete " + id);
	$('#tr_'+index).remove();
	var rowCount = $('#tblStock tr').length;
	if(rowCount == 2){
		$("#tblStock").css("display","none");
	}
}

function st_CheckOut(){
	
	var item = new Array();
	var amount = new Array();

	$("input[name^=st_i]").each(function(i) {
		//alert(i+":"+$(this).val());
		item[i] = $(this).val();
	});

	$("input[name^=st_a]").each(function(i) {
		//alert(i+":"+$(this).val());
		amount[i] = $(this).val();
	});
	
	//for(i=0;i<item.length;i++){
	//	alert(item[i]+amount[i]);
	//}
	
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=addBuyStock",
	   data: { "item[]" : item , "amount[]" : amount , "t" : Math.random() },
	   success: function(msg){
		   if(msg.match("orderid:")){
				alert("Success");				
		   }else{
				alert(msg);
		   }
			Logoshop_Main();
	   }
	});	
}

function delStock(id){
	if(confirm("Are You Sure to Delete IT ?")){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=delStock",
		   data: "&id="+id+"&t="+Math.random(),
		   success: function(msg){
				alert(msg);
				Logoshop_Main();
		   }
		});
	}
}

function Logoshop_Report(){	
	$.ajax({
	   type: "POST",
	   url: "logoshop_report.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function log_buy_report(){
	$.ajax({
	   type: "POST",
	   url: "log_buy_report.php",
	   data: "&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function editAccess(){	
	$.ajax({
	   type: "POST",
	   url: "editAccess.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function userManage(){	
	$.ajax({
	   type: "POST",
	   url: "userManage.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function editUser(id){
	$.ajax({
	   type: "POST",
	   url: "user.php",
	   data: "id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function delUser(id){

	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=delUser",
	   data: "id="+id+"&t="+Math.random(),
	   success: function(msg){
		   userManage();
	   }
	});
}

function newUser(){
	$.ajax({
	   type: "POST",
	   url: "user.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function update_Report_buy(){
	var m = $("#log_month").val();
	var y = $("#log_year").val();
	$.ajax({
	   type: "POST",
	   url: "log_buy_report.php?cmd=x",
	   data: "&m="+m+"&y="+y+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function log_stock_report(){
	$.ajax({
	   type: "POST",
	   url: "log_stock_report.php",
	   data: "&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function update_stock_report(){
	$.ajax({
	   type: "POST",
	   url: "log_stock_report.php?cmd=x",
	   data: "&cat_id="+$("#catStock").val()+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function log_sell_report(){	
	$.ajax({
	   type: "POST",
	   url: "log_sell_report.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function updateSellReport(){
	var dt = $("#sell_date").val();
	$.ajax({
	   type: "POST",
	   url: "log_sell_report.php?cmd=update",
	   data: "&dt="+dt+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function log_sum_report(){	
	$.ajax({
	   type: "POST",
	   url: "log_sum_report.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function updateSumReport(){
	var dt = $("#sum_date").val();
	$.ajax({
	   type: "POST",
	   url: "log_sum_report.php?cmd=update",
	   data: "&dt="+dt+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function showAccess(){
	var id = $("#access_user").val();

	if(id != ""){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=getAccess",
		   data: "&id="+id+"&t="+Math.random(),
		   success: function(msg){
			   $("#showAccess").html(msg);
		   }
		});
	}else{
		$("#showAccess").html("");
	}
}

function AUpdate(){
	$(this).attr('checked');

	var a_read = $("#a_read").attr('checked');
	var b_read = $("#b_read").attr('checked');
	var c_read = $("#c_read").attr('checked');
	var d_read = $("#d_read").attr('checked');
	var e_edit = $("#e_edit").attr('checked');
	var f_delete = $("#f_delete").attr('checked');
	var g_read = $("#g_read").attr('checked');
	var h_read = $("#h_read").attr('checked');
	var i_read = $("#i_read").attr('checked');
	var j_read = $("#j_read").attr('checked');
	var k_read = $("#k_read").attr('checked');
	var l_read = $("#l_read").attr('checked');
	var m_read = $("#m_read").attr('checked');
	var n_read = $("#n_read").attr('checked');
	var o_read = $("#o_read").attr('checked');
	var p_read = $("#p_read").attr('checked');
	var q_delete = $("#q_delete").attr('checked');


	var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;

	if(a_read == "checked"){
		a_read = "1";
	}else{
		a_read = "0";
	}

	if(b_read == "checked"){
		b_read = "1";
	}else{
		b_read = "0";
	}

	if(c_read == "checked"){
		c_read = "1";
	}else{
		c_read = "0";
	}

	if(d_read == "checked"){
		d_read = "1";
	}else{
		d_read = "0";
	}

	if(e_edit == "checked"){
		e_edit = "1";
	}else{
		e_edit = "0";
	}

	if(f_delete == "checked"){
		f_delete = "1";
	}else{
		f_delete = "0";
	}

	if(g_read == "checked"){
		g_read = "1";
	}else{
		g_read = "0";
	}

	if(h_read == "checked"){
		h_read = "1";
	}else{
		h_read = "0";
	}

	if(i_read == "checked"){
		i_read = "1";
	}else{
		i_read = "0";
	}

	if(j_read == "checked"){
		j_read = "1";
	}else{
		j_read = "0";
	}

	if(k_read == "checked"){
		k_read = "1";
	}else{
		k_read = "0";
	}

	if(l_read == "checked"){
		l_read = "1";
	}else{
		l_read = "0";
	}

	if(m_read == "checked"){
		m_read = "1";
	}else{
		m_read = "0";
	}

	if(n_read == "checked"){
		n_read = "1";
	}else{
		n_read = "0";
	}

	if(o_read == "checked"){
		o_read = "1";
	}else{
		o_read = "0";
	}

	if(p_read == "checked"){
		p_read = "1";
	}else{
		p_read = "0";
	}

	if(q_delete == "checked"){
		q_delete = "1";
	}else{
		q_delete = "0";
	}

	var data = a_read+b_read+c_read+d_read+e_edit+f_delete+g_read+h_read+i_read+j_read+k_read+l_read+m_read+n_read+o_read+p_read+q_delete;
	
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=updateAccess",
	   data: "&id="+$("#access_user").val()+"&data="+data+"&t="+Math.random(),
	   success: function(msg){
		   alert(msg);
		   editAccess();
	   }
	});

}



/*  New Menu */

/*   ยืนยันรับของ   */
function acceptOrder(){
	$.ajax({
	   type: "POST",
	   url: "acceptOrder.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function acceptDetail(id){
	$.ajax({
	   type: "POST",
	   url: "accept_detail.php",
	   data: "id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function checkAccept(sendid){
	var unknow_data = "";
	var i = 0;
	$("#a_unknow tr").each( function(){
		var rowid = $(this).attr('id');
		if(rowid != null){
			if(rowid.match("a_rows_")){
				var id = rowid.replace("a_rows_","");
				var barcode = $("#a_barcode_"+id).val();
				var product = $("#a_product_"+id).val();
				var amount = $("#a_amount_"+id).val();
				var unit = $("#a_unit_"+id).val();

				if(product != "" && amount != ""){
					if(i > 0){
						unknow_data += ";"+barcode+":"+product+":"+amount+":"+unit;	
					}else{
						unknow_data += barcode+":"+product+":"+amount+":"+unit;
					}
				}
				i += 1;
			}
		}
		
	});
	
	var error = 0;
	var data="";
	var index = 0;
	$("#tbl_accept_list input:text").each(function() {
		var tmp = $(this).val();
		var id = $(this).attr('id').split("_");
		if($("#accept_check_b_"+id[2]+":checked").is(':checked')){
			
			if(tmp != null){
				if($("#real_amount_"+id[2]).val() != ""){
					if(index > 0){
						data += ";"+id[2]+":"+$("#proid_"+id[2]).val()+":"+$("#pro_amount_"+id[2]).val()+":"+$("#real_amount_"+id[2]).val();
					}else{
						data += id[2]+":"+$("#proid_"+id[2]).val()+":"+$("#pro_amount_"+id[2]).val()+":"+$("#real_amount_"+id[2]).val();
					}
					index++;
				}else{
					error += 1;
				}
				//alert(id[2]);
			}else{
				if($("#accept_check_b_"+id[2]+":checked").is(':checked')){
					//alert("Please Fill Amount");
					$("#real_amount_"+id[2]).focus();
				}
			}
		}
	});

	if(error > 0){
		alert("Invalid Value");					
	}else{
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=acceptOrder",
		   data: "&sendid="+sendid+"&data="+data+"&unknow_data="+unknow_data+"&t="+Math.random(),
		   success: function(msg){
			   if(msg.match("Success")){
					alert("Success");
					acceptOrder();
			   }else{
					prompt("Alert",msg);
			   }
			   
		   }
		});
	}
}

function newRowUnknow(){
	var index = parseInt($("#a_last_row").val());

	var item = "";
	$("#a_row_"+index).html('<a href="#" onclick="delAccRow(\''+index+'\')">Delete</a>');
	index += 1;

	item = '<tr id="a_rows_'+index+'"><td><input type="text" id="a_barcode_'+index+'"></td>';
	item += '<td><input type="text" id="a_product_'+index+'"></td>';
	item += '<td><input type="text" id="a_amount_'+index+'"></td>';
	item += '<td><input type="text" id="a_unit_'+index+'"></td>';
	item += '<td id="a_row_'+index+'"><input type="button" value="Add" onclick="newRowUnknow()"></td></tr>';
	
	$("#a_unknow tr:last").after(item);
	$("#a_last_row").val(index)
}

function delAccRow(id){
	$("#a_rows_"+id).remove();
}







/*    ส่งของกลับฝั่ง   */
function sendOrderOffice(){
	$.ajax({
	   type: "POST",
	   url: "sendOrdertoOffice.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function OfficeStore(){
	$.ajax({
	   type: "POST",
	   url: "storeOffice.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function buyOfficeStock(){
	$.ajax({
	   type: "POST",
	   url: "buystockoffice.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function removeOfficeStock(){
	$.ajax({
	   type: "POST",
	   url: "removestockoffice.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function updateSendItem(){
	var id = $("#send_cat").val();
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=updateSendItem",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   $("#send_show_item").html(msg);
	   }
	});
}

function updateSendOName(){
	$("#send_item option:selected").each(function() {
		$("#send_name").val($(this).text());
		
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=getUnitcall",
		   data: "&id="+$("#send_item").val()+"t="+Math.random(),
		   success: function(msg){
			   if(msg != ""){
					var id = msg.split(":");

					var tmp = $("#send_item").val().split(":");
					//alert(tmp[0]+":"+tmp[1]);
					if(tmp[1] == ""){
						alert("สินค้านี้ยังไม่มี Barcode");
						$("#btnAddtmpSendIsland").attr('disabled', 'disabled');
					}else{
						$("#sendBarcode").val(tmp[1]);
						$('#send_unit option').removeAttr('selected').filter('[value=' + id[0] + ']').attr('selected', true);    
						$("#send_unittext").val(id[1]);
						$("#send_amount").val("");
						$("#send_amount").focus();
						$("#btnAddtmpSendIsland").removeAttr("disabled");
					}

			   }
		   }
		});
	});
}

function addrowSendOffice(){
	var x = $("#rowSendOffice").html();

	var send_item = $("#send_item").val();
	var send_amount = parseInt($("#send_amount").val());
	var send_index = $("#send_index").val();
	var send_unit = $("#send_unit").val();
	var send_desc = $("#send_desc").val();

	var send_name = $("#send_name").val();

	var item = "";
	var chk = true;
	var className = "";

	
	if(send_item != "" && send_amount > 0 && send_unit != ""){
		$("#sendOffice_list").css("display","inline");
	
		$("input[name^=send_i]").each(function(i) {
			var xval = $(this).val();
			if(send_item == xval){
				var lastVal = parseInt($("input[name^=send_a]").eq(i).val());
				send_amount += lastVal;
				var tr_id = $(this).parents("tr").attr("id");			
				item = '<tr id="tr_'+send_index+'"><td class="a">'+send_name+'</td><td class="b">'+send_amount+'</td><td class="c">'+send_unit+"</td><td>"+send_desc+"</td>";
				item += '<td align="center"><input type="hidden" name="send_i[]" value="'+send_item+'">';
				item += "<input type='hidden' name='send_a[]' value='"+send_amount+"'>";
				item += "<input type='hidden' name='send_u[]' value='"+send_unit+"'>";
				item += "<input type='hidden' name='send_d[]' value='"+send_desc+"'>";
				item += '<a href="#" onclick="delSendOfficeItem(\''+send_index+'\')">del</a></td></tr>';
				$('#'+tr_id).replaceWith(item);
				chk = false;
			}
		});
		
		if(chk){
			item = '<tr id="tr_'+send_index+'"><td class="a">'+send_name+'</td><td class="b">'+send_amount+'</td><td class="c">'+send_unit+"</td><td>"+send_desc+"</td>";
			item += '<td align="center"><input type="hidden" name="send_i[]" value="'+send_item+'">';
			item += "<input type='hidden' name='send_a[]' value='"+send_amount+"'>";
			item += "<input type='hidden' name='send_u[]' value='"+send_unit+"'>";
			item += "<input type='hidden' name='send_d[]' value='"+send_desc+"'>";
			item += '<a href="#" onclick="delSendOfficeItem(\''+send_index+'\')">del</a></td></tr>';
			$('#sendOffice_list tr:first').next().after(item);
			$('#sendOffice_list tr:first').next().next().hide().fadeIn("slow")
		}
		$("#send_index").val(parseInt(send_index)+1);
		document.getElementById('btnCheckOut').disabled = false;
		$("#send_item").val("");
		$("#send_amount").val("");
		$("#send_unit").val("");
		$("#send_desc").val("");
		//alert($("#or_list").html());
	}else{
		alert("Invalid Value");
	}
}

function delSendOfficeItem(index){
	
	$('#tr_'+index).remove();
	var rowCount = $('#send_list tr').length;
	if(rowCount == 2){
		$("#send_list").css("display","none");
	}
}

function sendCheckOut(){
	var item = new Array();
	var amount = new Array();
	var unit = new Array();
	var desc = new Array();

	$("input[name^=send_i]").each(function(i) {
		//alert(i+":"+$(this).val());
		item[i] = $(this).val();
	});

	$("input[name^=send_a]").each(function(i) {
		//alert(i+":"+$(this).val());
		amount[i] = $(this).val();
	});

	$("input[name^=send_u]").each(function(i) {
		//alert(i+":"+$(this).val());
		unit[i] = $(this).val();
	});

	$("input[name^=send_d]").each(function(i) {
		//alert(i+":"+$(this).val());
		desc[i] = $(this).val();
	});

	//alert(item.length);

	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=addSendOffice",
	   data: { "item[]" : item , "amount[]" : amount , "unit[]" : unit , "desc[]" : desc , "t" : Math.random() },
	   success: function(msg){
		   if(msg.match("ok:")){
				alert("Success");
				var id = msg.split(":");
				loadEffect('Download Order <a href="sendOfficeExcel.php?id='+id[1]+'"><img src="images/excel_icon.jpg" width="20"></a>');
				//getExcelForm();
		   }else{
				alert(msg);
		   }
			//loadUnitManage();
	   }
	});
}


/*  ส่งของไปเกาะ */
function sendOrderToIsland(){
	$.ajax({
	   type: "POST",
	   url: "sendOrdertoIsland.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}


function addrowSendIsland(){
	var tmp = $("#send_item").val().split(":");
	var send_item = tmp[0];
	var send_name = $("#send_name").val();
	var send_amount = $("#send_amount").val();
	var send_index = $("#send_index").val();
	var send_unit = $("#send_unit").val();
	var send_unittext = $("#send_unittext").val();
	var send_barcode = $("#sendBarcode").val();
	

	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=addSendCart",
	   data: "&send_item="+send_item+"&send_barcode="+send_barcode+"&send_amount="+send_amount+"&send_name="+send_name+"&send_unit="+send_unit+"&send_unittext="+send_unittext+"&t="+Math.random(),
	   success: function(msg){
			if(msg.match("ok:")){
				updateSendCart();
			}else{
				alert(msg);
			}
		}
	});
}

function updateSendCart(){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=updateSendCart",
	   data: "t="+Math.random(),
	   success: function(msg){
		   //alert(msg);
			$('#sendOffice_list').html(msg);
			//alert($("#or_list").html());
	   }
	});
}

function delSendIslandItem(index){

	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=removeSendCart",
	   data: "&id="+index+"&t="+Math.random(),
	   success: function(msg){
			if(msg.match("ok:")){
				$('#tr_'+index).remove();
				var rowCount = $('#send_lists tr').length;
				if(rowCount == 2){
					$("#send_lists").css("display","none");
					$("#btnSendCheckOut").css("display","none");
				}
			}else{
				alert(msg);
			}
		}
	});
}

function updateSendIslandUnit(){
	$("#send_unit option:selected").each(function() {
		$("#send_unittext").val($(this).text());
	});
}

function OLDaddrowSendIsland(){
	var x = $("#rowSendOffice").html();

	var send_item = $("#send_item").val();
	var send_amount = $("#send_amount").val();
	var send_index = $("#send_index").val();
	var send_unit = $("#send_unit").val();

	var send_name = $("#send_name").val();

	var item = "";
	var chk = true;
	var className = "";

	
	if(send_item != "" && send_amount > 0 && send_unit != ""){
		$("#sendOffice_list").css("display","inline");
	
		$("input[name^=send_i]").each(function(i) {
			var xval = $(this).val();
			if(send_item == xval){
				var lastVal = parseInt($("input[name^=send_a]").eq(i).val());
				send_amount += lastVal;
				var tr_id = $(this).parents("tr").attr("id");			
				item = '<tr id="tr_'+send_index+'"><td class="a">'+send_name+'</td><td class="b">'+send_amount+'</td><td class="c">'+send_unit+"</td>";
				item += '<td align="center"><input type="hidden" name="send_i[]" value="'+send_item+'">';
				item += "<input type='hidden' name='send_a[]' value='"+send_amount+"'>";
				item += "<input type='hidden' name='send_u[]' value='"+send_unit+"'>";
				item += '<a href="#" onclick="delSendOfficeItem(\''+send_index+'\')">del</a></td></tr>';
				$('#'+tr_id).replaceWith(item);
				chk = false;
			}
		});
		
		if(chk){
			item = '<tr id="tr_'+send_index+'"><td class="a">'+send_name+'</td><td class="b">'+send_amount+'</td><td class="c">'+send_unit+"</td>";
			item += '<td align="center"><input type="hidden" name="send_i[]" value="'+send_item+'">';
			item += "<input type='hidden' name='send_a[]' value='"+send_amount+"'>";
			item += "<input type='hidden' name='send_u[]' value='"+send_unit+"'>";
			item += '<a href="#" onclick="delSendOfficeItem(\''+send_index+'\')">del</a></td></tr>';
			$('#sendOffice_list tr:first').next().after(item);
			$('#sendOffice_list tr:first').next().next().hide().fadeIn("slow")
		}
		$("#send_index").val(parseInt(send_index)+1);
		document.getElementById('btnCheckOut').disabled = false;
		$("#send_item").val("");
		$("#send_amount").val("");
		$("#send_unit").val("");
		//alert($("#or_list").html());
	}else{
		alert("Invalid Value");
	}
}

function sendIslandCheckOut(){
	if(confirm('Do you want to Send ?')==true){
		var item = new Array();
		var amount = new Array();
		var unit = new Array();
		var barcode = new Array();

		

		$("input[name^=send_i]").each(function(i) {
			//alert(i+":"+$(this).val());
			item[i] = $(this).val();
		});

		$("input[name^=send_a]").each(function(i) {
			//alert(i+":"+$(this).val());
			amount[i] = $(this).val();
		});

		$("input[name^=send_u]").each(function(i) {
			//alert(i+":"+$(this).val());
			unit[i] = $(this).val();
		});

		$("input[name^=send_b]").each(function(i) {
			//alert(i+":"+$(this).val());
			barcode[i] = $(this).val();
		});

		//alert(item.length);

		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=addSendIsland",
		   data: { "item[]" : item , "amount[]" : amount , "unit[]" : unit , "barcode[]" : barcode , "t" : Math.random() },
		   success: function(msg){
			   if(msg.match("ok:")){
					alert("Success");
					var id = msg.split(":");
					loadEffect('Download Send Order <a href="sendIslandExcel.php?id='+id[1]+'"><img src="images/excel_icon.jpg" width="20"></a>');
					//getExcelForm();
			   }else{
					alert(msg);
			   }
				//loadUnitManage();
		   }
		});
	}
}

/* รายการชองที่ส่งไม่ครบ*/
function orderNotAccept(){
	$.ajax({
	   type: "POST",
	   url: "unfinished_order.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

/* รับของจากเกาะ*/
function receiveFromIsland(){
	$.ajax({
	   type: "POST",
	   url: "receiveFromIsland.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

/* log การรับของจากฝั่ง*/
function History_Receive(){
	$.ajax({
	   type: "POST",
	   url: "HistoryReceive.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function receiveUnknow(){
	$.ajax({
	   type: "POST",
	   url: "receiveUnknow.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function showUnknow(id){
	$.ajax({
	   type: "POST",
	   url: "showUnknow.php",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function showAccept(id){
	$.ajax({
	   type: "POST",
	   url: "showNotAccept.php",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function confirmSended(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=confirmSended",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		    if(msg.match("ok:")){
				alert("Success");
				orderNotAccept();
				//getExcelForm();
		   }else{
				alert(msg);
		   }
	   }
	});
}

function searchProduct(){
	var str = $("#searchText").val();
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=searchProduct",
	   data: "&search="+str+"&t="+Math.random(),
	   success: function(msg){
		   $("#productTable").html(msg);
	   }
	});
}

function viewAcceptDetail(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=viewAcceptDetail",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function printNotAccept(id){
	var url = "print_view.php?cmd=printNotAccept&id="+id;
	//var windowName = "popUp";
	//$(this).attr("name"); 
	//var windowWidth = 800;
	//var windowHeight = 600;
	//var windowLeft = parseInt((screen.availWidth/2) - (windowWidth/2));
	//var windowTop = parseInt((screen.availHeight/2) - (windowHeight/2));
	//var windowSize = "width=" + windowWidth + ",height=" + windowHeight + "left=" + windowLeft + ",top=" + windowTop + "screenX=" + windowLeft + ",screenY=" + windowTop;

	//window.open(url, windowName, windowSize);
	//event.preventDefault();
	popupWindow = window.open( url, 'popUpWindow', 'height=600,width=800,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=no,directories=no,status=yes');
}

function clearUnfinished(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=clearUnfinished",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   if(msg.match("ok:")){
				alert("Success");				
				orderNotAccept();
		   }else{
				alert(msg);
		   }
	   }
	});
}

function delUnfinished(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=delUnfinished",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   if(msg.match("ok:")){
				alert("Success");				
				orderUnfinished();
		   }else{
				alert(msg);
		   }
	   }
	});
}

function orderUnfinished(){
	$.ajax({
	   type: "POST",
	   url: "listUnfinished.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   
		   loadEffect(msg)
	   }
	});
}

function newPopup(url) { 

}

function repairSend(){
	$.ajax({
	   type: "POST",
	   url: "repairSend.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function newRepair(){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=newRepair",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg);
	   }
	});
}

function insertRepair(){
	var product = $("#product").val();
	var qty = $("#qty").val();
	var detail = $("#detail").val();
	var id = parseInt($("#idRow").val());

	$("#showNewRepair").show('slow');
	
	var strData = '<tr id="row_'+id+'"><td id="pro_'+id+'">'+product+'</td><td id="qty_'+id+'">'+qty+'</td><td id="det_'+id+'">'+detail+'</td><td><a href="#" id="del_'+id+'" onclick="delRepairItem(\''+id+'\')">del</a></td></tr>';
	//alert(strData);
	$('#repairTable tbody').append(strData);
	//alert($('#repariTable tbody').html());
	id += 1;
	$("#idRow").val(id);

	$("#product").val("");
	$("#qty").val("");
	$("#detail").val("");
}

function delRepairItem(rowId){
	//alert("row_"+id);
	var id = parseInt($("#idRow").val());
	var str = "row_"+rowId;
	$("#repairTable tbody tr").each(function(){
		if($(this).attr("id") == str){
			$(this).remove();
			id-=1;
			$("#idRow").val(id);
			if(id < 1){
				$("#showNewRepair").hide('slow');
			}
			return;
		}
	});
}

function addRepair(){
	var myArray  = new Array();
	var id = parseInt($("#idRow").val());
	var indexS = 1;
	var product = "";
	var qty = "";
	var detail = "";
	$("#repairTable tbody tr td").each(function(counter){
		//$(this+" td").each(function(){
			//alert(counter%3);
			if(counter%4 == 0){
				product = $(this).attr("id");
			}else if(counter%4 == 1){
				qty = $(this).attr("id");
			}else if(counter%4 == 2){
				detail = $(this).attr("id");
				indexS = 1;
				//alert($("#"+product).html());

				//alert(product+":"+qty+":"+detail);
				
				myArray.push({
					'product': $("#"+product).html(),
					'qty': $("#"+qty).html(),
					'detail': $("#"+detail).html()
				});
				//return true;
			}else{
				return true;
			}
			indexS++;
			//alert($(this).html());
			
		//});
		
		

	});

	var ajaxData = { myArray : JSON.stringify(myArray) };
	//alert(ajaxData);
	
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=addRepair",
	   data: ajaxData,
	   success: function(msg){
			if(msg.match("OK")){
				repairSend();
			}else{
				alert(msg);
			}
		   
	   }
	});
}

function repairView(){
	$.ajax({
	   type: "POST",
	   url: "repairView.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function repairAdd(){
	$.ajax({
	   type: "POST",
	   url: "repairAdd.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function repairAddNew(){
	$.ajax({
	   type: "POST",
	   url: "repair_addnew.php",
	   data: "t="+Math.random(),
	   success: function(msg){
		   loadEffect(msg)
	   }
	});
}

function repairSaveNew(){
	var product = $("#rproduct").val();
	var senddate = $("#rsenddate").val();
	var place = $("#rplace").val();
	var billno = $("#rbillno").val();
	var receivedate = $("#rreceivedate").val();
	var sendby = $("#rsendby").val();
	var urlsend = "&product="+product+"&senddate="+senddate+"&place="+place+"&billno="+billno+"&receivedate="+receivedate+"&sendby="+sendby+"&t="+Math.random();
	
	//alert(urlsend);

	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=repairSaveNew",
	   data: urlsend,
	   success: function(msg){
		   //alert(msg);
		   if(msg.match("OK")){
				alert("Saved");
				repairAdd();
			}else{
				alert(msg);
			}
	   }
	});
}


function finishRepairAdd(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=finishRepairAdd",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   if(msg.match("OK")){
				alert("Accepted");
				repairAdd();
			}else{
				alert(msg);
			}
	   }
	});
}

function delRepairAdd(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=delRepairAdd",
	   data: "&id="+id+"&t="+Math.random(),
	   success: function(msg){
		   if(msg.match("OK")){
				alert("Deleted");
				repairAdd();
			}else{
				alert(msg);
			}
	   }
	});
}

function repairConfirm(id){
	$.ajax({
	   type: "POST",
	   url: "process.php?cmd=confirmRepair",
	   data: "id="+id+"&t="+Math.random(),
	   success: function(msg){
			if(msg.match("OK")){
				repairView();
			}else{
				alert(msg);
			}
	   }
	});
}

function testCheckOut(){

	if(confirm('Do you want to Order ?')==true){

		var item = new Array();
		var amount = new Array();
		var unit = new Array();
		var quick = new Array();

		$('.checkboxSelector:checked').each(function(i){
			var tmpid = $(this).attr("id").split("_");
			var tpro_id,tamt,tunit,tquick;
			tpro_id = tmpid[1];
			tamt = $("#amount_"+tpro_id).val();
			tunit = $("#unit_"+tpro_id).val();
			tquick = $("#quick_"+tpro_id).prop('checked');
				
			if(tquick){
				tquick = "1";
			}else{
				tquick = "0";
			}

			//alert(tpro_id+" "+tamt+" "+tunit+":"+tquick);
			item[i] = tpro_id;
			amount[i] = tamt;
			unit[i] = tunit;
			quick[i] = tquick;
		});
		

		
		//alert(item.length);
		
		if(item.length > 0){
			$.ajax({
			   type: "POST",
			   url: "process.php?cmd=addCart",
			   data: { "item[]" : item , "amount[]" : amount , "unit[]" : unit , "quick[]" : quick , "t" : Math.random() },
			   success: function(msg){
				   if(msg.match("orderid:")){
						alert("Success");
						var id = msg.split(":");
						loadEffect('Download Order <a href="excel.php?id='+id[1]+'"><img src="images/excel_icon.jpg" width="20"></a>');
						//getExcelForm();
				   }else{
						alert(msg);
				   }
					//loadUnitManage();
			   }
			});
		}else{
			alert("No Data");
		}
	}
}